//
//  ItemDetailTopView.m
//  DanTang
//
//  Created by honeyrr on 2017/9/24.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "ItemDetailTopView.h"
#import "ItemDetailCollectionViewCell.h"

//cell静态标识
static NSString * const itemDetailCollectionCellID = @"itemDetailCollectionCellID";

@interface ItemDetailTopView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

//图片数组
@property(nonatomic, strong) NSArray *imageUrls;
//表格
@property(nonatomic, strong) UICollectionView *collectionView;
//底部滑动图片的分页
@property(nonatomic, strong) UIPageControl *pageControl;
//标题
@property(nonatomic, strong) UILabel *titleLabel;
//价格
@property(nonatomic, strong) UILabel *priceLabel;
//描述
@property(nonatomic, strong) UILabel *describeLabel;
@end

@implementation ItemDetailTopView


#pragma mark - 重写父类initWithFrame方法
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}
#pragma mark - 设置top布局界面
-(void) setUpUI
{
    [self addSubview:self.collectionView];
    //pageControl frame
    CGFloat pageW = 100;
    CGFloat pageH = 40;
    CGFloat pageX = 0;
    CGFloat pageY = self.collectionView.mr_height - pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
    //内容格式
    self.pageControl.contentMode = UIViewContentModeCenter;
    [self addSubview:self.pageControl];
    
    //title、price、describe frame
    self.titleLabel.frame = CGRectMake(5, CGRectGetMaxY(self.collectionView.frame) + 5, DTScreenW - 10, 25);
    [self addSubview:self.titleLabel];
    self.priceLabel.frame = CGRectMake(5, CGRectGetMaxY(self.titleLabel.frame) + 5, DTScreenW - 10, 25);
    [self addSubview:self.priceLabel];
    self.describeLabel.frame = CGRectMake(5, CGRectGetMaxY(self.priceLabel.frame) + 5, DTScreenW - 10, 45);
    [self addSubview:self.describeLabel];
}
#pragma mark - 重写item setter方法
-(void)setItem:(NSDictionary *)item
{
    _item = item;
    //设置图片数组
    self.imageUrls = item[@"image_urls"];
    //设置pageControl的页数
    self.pageControl.numberOfPages = self.imageUrls.count;
    [self.collectionView reloadData];
    self.pageControl.mr_centerX = self.collectionView.mr_centerX;
    
    self.titleLabel.text = item[@"name"];
    self.priceLabel.text = [NSString stringWithFormat:@" ¥ %@",item[@"price"]];
    self.describeLabel.text = item[@"description"];
}

#pragma mark - 懒加载
-(NSArray *)imageUrls
{
    if (!_imageUrls) {
        _imageUrls = [NSArray array];
    }
    return _imageUrls;
}
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        //设置item大小
        layout.itemSize = CGSizeMake(DTScreenW, 375);
        //间隙
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        //滚动方向
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        UICollectionView *cv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, DTScreenW, 375) collectionViewLayout:layout];
        //代理和数据源
        cv.delegate = self;
        cv.dataSource = self;
        //是否显示滚动条
        cv.showsVerticalScrollIndicator = NO;
        cv.showsHorizontalScrollIndicator = NO;
        //翻页以及拉伸效果
        cv.pagingEnabled = YES;
        cv.bounces = NO;
        //注册cell
        [cv registerNib:[UINib nibWithNibName:NSStringFromClass([ItemDetailCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:itemDetailCollectionCellID];
        cv.backgroundColor = [UIColor whiteColor];
        _collectionView = cv;
    }
    return _collectionView;
}
-(UIPageControl *)pageControl
{
    if (!_pageControl) {
        UIPageControl *pc = [[UIPageControl alloc]init];
        pc.currentPageIndicatorTintColor = [UIColor whiteColor];
        pc.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl = pc;
    }
    return _pageControl;
}
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.numberOfLines = 0;
        _titleLabel = titleLabel;
    }
    return _titleLabel;
}
-(UILabel *)priceLabel
{
    if (!_priceLabel) {
        UILabel *priceLabel = [[UILabel alloc]init];
        priceLabel.textColor = MRNavTabRGBColor;
        priceLabel.numberOfLines = 0;
        priceLabel.font = [UIFont systemFontOfSize:16];
        _priceLabel = priceLabel;
    }
    return _priceLabel;
}
-(UILabel *)describeLabel
{
    if (!_describeLabel) {
        UILabel *describeLabel = [[UILabel alloc]init];
        describeLabel.numberOfLines = 0;
        describeLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        describeLabel.font = [UIFont systemFontOfSize:15];
        _describeLabel = describeLabel;
    }
    return _describeLabel;
}


#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageUrls.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemDetailCollectionCellID forIndexPath:indexPath];
    [cell.itemDetailImg sd_setImageWithURL:self.imageUrls[indexPath.row]];
    return cell;
}
#pragma mark - 滑动scrollView移动pageControl
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取偏移量
    CGFloat offset = scrollView.contentOffset.x;
    //通过偏移量除scrollView宽 得到页数
    CGFloat pageNum = offset / scrollView.mr_width;
    self.pageControl.currentPage = (NSInteger) pageNum;
}
@end
